package club.emergency.wechat.authorization.operate.consumer.controller;

import club.emergency.wechat.authorization.operate.consumer.service.AuthorizationOperateConsumer;
import club.emergency.wechat.common.response.ResponseEntity;
import club.emergency.wechat.common.utils.JsonConvert;
import club.emergency.wechat.model.role.Role;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * Create with IntelliJ IDEA
 * Project Name: wechat
 * Package Name: club.emergency.wechat.authorization.operate.consumer.controller
 * Date: 2019/3/19
 *
 * @author: 快乐的小菌菌
 * Description:
 */
@Api("权限接口")
@RestController
@RequestMapping("/authorization-operate")
public class AuthorizationOperateController {

    private AuthorizationOperateConsumer authorizationOperateConsumer;

    public AuthorizationOperateController(AuthorizationOperateConsumer authorizationOperateConsumer) {
        this.authorizationOperateConsumer = authorizationOperateConsumer;
    }

    @ApiOperation("新增角色")
    @PostMapping("/saveRole")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleName", value = "角色名称", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "authorizationIds", value = "多个权限id，逗号分隔", dataType = "string", paramType = "query")
    })
    public ResponseEntity saveRole(
            @RequestParam("roleName") String roleName,
            @RequestParam(value = "authorizationIds", required = false) String authorizationIds
    ) {
        Role role = new Role();
        role.setRoleName(roleName);
        return new JsonConvert<ResponseEntity>().jsonDecode(authorizationOperateConsumer.saveRole(role, authorizationIds), ResponseEntity.class);
    }

    @ApiOperation("编辑角色")
    @PostMapping("/editRole")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "roleName", value = "角色名称", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "authorizationIds", value = "多个权限id，逗号分隔", dataType = "string", paramType = "query")
    })
    public ResponseEntity editRole(
            @RequestParam("id") Long id,
            @RequestParam("roleName") String roleName,
            @RequestParam(value = "authorizationIds", required = false) String authorizationIds
    ) {
        Role role = new Role();
        role.setId(id);
        role.setRoleName(roleName);
        return new JsonConvert<ResponseEntity>().jsonDecode(authorizationOperateConsumer.editRole(role, authorizationIds), ResponseEntity.class);
    }

    @ApiOperation("删除角色")
    @PostMapping("/removeRole")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", dataType = "long", paramType = "query")
    })
    public ResponseEntity removeRole(
            @RequestParam("id") Long id
    ) {
        return new JsonConvert<ResponseEntity>().jsonDecode(authorizationOperateConsumer.removeRole(id), ResponseEntity.class);
    }

    @ApiOperation("后台人员绑定角色")
    @PostMapping("/bindManager")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "managerId", value = "后台用户id", dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "roleIds", value = "角色ids，逗号分隔（可以是一个）", dataType = "string", paramType = "query")
    })
    public ResponseEntity bindManager(
            @RequestParam("managerId") Long managerId,
            @RequestParam("roleIds") String roleIds
    ) {
        return new JsonConvert<ResponseEntity>().jsonDecode(authorizationOperateConsumer.bindManager(managerId, roleIds), ResponseEntity.class);
    }
}
